<?php

/**
 * @copyright 2011 by Aaron, Brice, Dan, Mike.  This program is part
 * of Hapinoy Project, which is free software. It comes with absolutely no
 * warranty. You can redistribute or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation (For
 * more information, see <http://www.gnu.org/licenses/).
 *
 * @version April 12, 2011
 *
 * @author Brice Rader
 */
/**
 * This is the database file.
 * It controls the province file to list all categories and
 * to add or delete a province into the database.
 * It is the only file that has the SQL queries and MySQL connections
 */
include_once('inc/functions.php');

class dbProvince {

    private $db;

    static function createTable() {
        //create table province
        $query = "
        create table province(
            provinceID      int             NOT NULL AUTO_INCREMENT,
            provinceName    varchar(45)     NOT NULL,
            constraint PK_Province PRIMARY KEY(provinceID)
            );
";
        connectToDatabase();
        $result = mysql_query($query);
        if ($result == false) {
            echo mysql_error();
            return false;
        }
        mysql_close();
        //Previous query was successful.
        return true;
    }

    /**
     * Gets all of the provinces in the database
     * @return provinces
     */
    function getAllProvince() {
        $query = "SELECT * FROM province";
        connectToDatabase( );
        $result = mysql_query($query);
        mysql_close();
        if ($result == false) {
            echo mysql_error( );
            return false;
        }

        //Query was successful.
        return $result;
    }

    /**
     *
     * @param province $c
     * @return new province added to database
     */
    static function addProvince($p) {
        if (!$p instanceof Province) {
            echo ("Invalid arguement insertInto ( ) function<br />");
            return false;
        }
        //Compose the INSERT INTO query srting. Primary keyvalue is missing.
        //Database server automatically generates a unique ID for the primary
        //key.
        $query = sprintf("INSERT INTO province VALUES (null, '%s')",
                        $c->getProvinceName()
        );
        connectToDatabase( );
        $result = mysql_query($query);
        printf("Province added has ID %d\n<br />", mysql_insert_id());
        mysql_close( );
        if ($result == false) {
            echo mysql_error ( );
            return false;
        }
        //query was successful.
        return true;
    }

    /**
     *
     * @param contact $c
     * @return checks to see what contacts are in the database
     */
    function checkProvinceID($p) {
        if (!($p instanceof Province)) {
            return false;
        }
        $provinceID = $p->getProvinceID();
        connectToDatabase();
        if (mysql_num_rows(mysql_query("SELECT * FROM province WHERE
                provinceID=$provinceID")) == false) {
            mysql_close();
            //nothing existsmysql_num_rows
            return false;
        } else {
            return true;
        }
    }

    /**
     *
     * @param $provinceID
     * @return ID
     */
    function getProvinceID($provinceID) {
        $p = new Province ();
        connectToDatabase();
        $result = mysql_query("SELECT * FROM province WHERE
                provinceID=$provinceID");
        mysql_close();
        if (!$result) {
            return false; //ID does not exist
        } else {
            $row = mysql_fetch_assoc($result);
            $p->setProvinceID($provinceID);
            return $p;
        }
    }

    /**
     *
     * @param $ProvinceName
     * @return Province
     */
    function getProvinceName($provinceName) {
        $p = new Province ();
        connectToDatabase();
        $result = mysql_query("SELECT * FROM province WHERE provinceName=
                $provinceName");
        mysql_close();
        if (!$result) {
            return false; //name does not exist
        } else {
            $row = mysql_fetch_assoc($result);
            $c->setProvinceName($provinceName);
            return $c;
        }
    }

    /**
     * Delete's province from database
     * @param contact $c
     * @return categories in database
     */
    function deleteProvinceID($c) {
        if (!($c instanceof Province)) {
            echo ("Invalid argument deleteFrom( ) function<br />");
            return false;
        }

        //Compose the DELETE FROM query string.
        $query = sprintf("DELETE FROM province WHERE provinceID = %d",
                        $c->getProvinceID()
        );
        connectToDatabase( );
        $result = mysql_query($query);
        mysql_close( );
        if ($result == false) {
            echo mysql_error( );
            return false;
        }

        //Query was successful.
        return true;
    }

}

?>
